home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / utils / xad / developer / sources / tools / xadundisk.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  11KB  |  383 lines

  1. #define NAME         "xadUnDisk"
  2. #define DISTRIBUTION "(Freeware) "
  3. #define REVISION     "4"
  4.  
  5. /* Programmheader
  6.  
  7.     Name:        xadUnDisk
  8.     Author:        SDI
  9.     Distribution:    Freeware
  10.     Description:    dearchives disk archives
  11.     Compileropts:    -
  12.     Linkeropts:    -gsi -l amiga
  13.  
  14.  1.0   18.11.98 : first version
  15.  1.1   23.12.98 : added support for FILEDESCRIPT
  16.  1.2   04.02.99 : added file corrupt message, new InfoText system and SAVETEXTS
  17.  1.3   16.02.99 : bug fix in report hook
  18.  1.4   28.02.99 : little bug fix in chunk hook
  19. */
  20.  
  21. #include <proto/xadmaster.h>
  22. #include <proto/exec.h>
  23. #include <proto/dos.h>
  24. #include <exec/memory.h>
  25. #include <dos/dosasl.h>
  26. #include <utility/hooks.h>
  27. #include "SDI_defines.h"
  28. #include "SDI_compiler.h"
  29. #define SDI_TO_ANSI
  30. #include "SDI_ASM_STD_protos.h"
  31.  
  32. #ifdef __SASC
  33.   #define xadmasterbase     xadMasterBase 
  34.   #define ASSIGN_XAD
  35. #else
  36.   struct xadMasterBase * xadMasterBase = 0;
  37.   #define ASSIGN_XAD     xadMasterBase = xadmasterbase;
  38. #endif
  39. struct DosLibrary *     DOSBase = 0;
  40. struct ExecBase *     SysBase  = 0;
  41.  
  42. #define PARAM    "FROM/A,TO,LOWCYL/N,HIGHCYL/N,ENTRY/N,PASSWORD,SAVETEXTS/K," \
  43.         "NE=NOEXTERN/S,INFO=LIST/S,SHOWTEXTS/S,OW=OVERWRITE/S,"    \
  44.         "IG=IGNOREGEOMETRY/S"
  45.  
  46. struct Args {
  47.   STRPTR from;
  48.   STRPTR to;
  49.   LONG * lowcyl;
  50.   LONG * highcyl;
  51.   LONG * entry;
  52.   STRPTR password;
  53.   STRPTR savetexts;
  54.   ULONG  noextern;
  55.   ULONG  info;
  56.   ULONG  showtexts;
  57.   ULONG  overwrite;
  58.   ULONG  ignoregeometry;
  59. };
  60.  
  61. ASM(ULONG) SAVEDS progrhook(REG(a0, struct Hook *),
  62.   REG(a1, struct xadProgressInfo *));
  63. struct Hook prhook = {{0,0},(ULONG (*)()) progrhook, 0, 0};
  64. void ShowTexts(struct xadTextInfo *ti);
  65. void SaveTexts(struct xadTextInfo *ti, STRPTR name);
  66.  
  67. ULONG start(void)
  68. {
  69.   ULONG ret = RETURN_FAIL;
  70.   struct DosLibrary *dosbase;
  71.  
  72.   SysBase = (*((struct ExecBase **) 4));
  73.   { /* test for WB and reply startup-message */
  74.     struct Process *task;
  75.     if(!(task = (struct Process *) FindTask(0))->pr_CLI)
  76.     {
  77.       WaitPort(&task->pr_MsgPort);
  78.       Forbid();
  79.       ReplyMsg(GetMsg(&task->pr_MsgPort));
  80.       return RETURN_FAIL;
  81.     }
  82.   }
  83.  
  84.   if((dosbase = (struct DosLibrary *) OpenLibrary("dos.library", 37)))
  85.   {
  86.     LONG err = 0;
  87.     struct xadMasterBase *xadmasterbase;
  88.  
  89.     DOSBase = dosbase;
  90.     if((xadmasterbase = (struct xadMasterBase *)
  91.     OpenLibrary("xadmaster.library", 1)))
  92.     {
  93.       LONG def = 1;
  94.       struct Args args;
  95.       struct RDArgs *rda;
  96.       
  97.       memset(&args, 0 , sizeof(struct Args));
  98.       args.entry = &def;
  99.  
  100.       ASSIGN_XAD
  101.       if((rda = ReadArgs(PARAM, (LONG *) &args, 0)))
  102.       {
  103.     if(args.to || args.info)
  104.     {
  105.       struct xadArchiveInfo *ai;
  106.     
  107.       if((ai = (struct xadArchiveInfo *)
  108.       xadAllocObjectA(XADOBJ_ARCHIVEINFO, 0)))
  109.       {
  110.         if(!(err = xadGetInfo(ai, XAD_INFILENAME, args.from,
  111.         XAD_NOEXTERN, args.noextern, args.password ? XAD_PASSWORD :
  112.         TAG_IGNORE, args.password, TAG_DONE)))
  113.         {
  114.           if(ai->xai_Flags & XADAIF_FILECORRUPT)
  115.             Printf("!!! The archive file has some corrupt data. !!!\n");
  116.           if(args.info)
  117.           {
  118.             struct xadDiskInfo *xdi;
  119.             Printf("ArchiverName:   %s\n", ai->xai_Client->xc_ArchiverName);
  120. #ifdef DEBUG
  121.             Printf("Password:       %s\n", ai->xai_Password ? ai->xai_Password : "<none>");
  122.             Printf("InSize:         %ld\n", ai->xai_InSize);
  123.             Printf("InPos:          %ld\n", ai->xai_InPos);
  124. #endif
  125.  
  126.             xdi = ai->xai_DiskInfo;
  127.             while(xdi)
  128.             {
  129.           if(xdi->xdi_EntryNumber != 1 || xdi->xdi_Next)
  130.                 Printf("\nEntry:          %ld\n", xdi->xdi_EntryNumber);
  131.               Printf("EntryInfo:      %s\n", xdi->xdi_EntryInfo ? xdi->xdi_EntryInfo : "<none>");
  132. #ifdef DEBUG
  133.               Printf("Flags:          ");
  134.               if(xdi->xdi_Flags & XADDIF_CRYPTED)
  135.                 Printf("XADDIF_CRYPTED ");
  136.               Printf(xdi->xdi_Flags ? "\n" : "<none>\n");
  137. #endif
  138.               Printf("SectorSize:     %ld\n", xdi->xdi_SectorSize);
  139.               Printf("Sectors:        %ld\n", xdi->xdi_TotalSectors);
  140.               Printf("Cylinders:      %ld\n", xdi->xdi_Cylinders);
  141.               Printf("CylSectors:     %ld\n", xdi->xdi_CylSectors);
  142.               Printf("Heads:          %ld\n", xdi->xdi_Heads);
  143.               Printf("TrackSectors:   %ld\n", xdi->xdi_TrackSectors);
  144.               Printf("LowCyl:         %ld\n", xdi->xdi_LowCyl);
  145.               Printf("HighCyl:        %ld\n", xdi->xdi_HighCyl);
  146.               if(xdi->xdi_Flags & XADDIF_CRYPTED)
  147.                 Printf("The entry is encrypted\n");
  148.               if(xdi->xdi_TextInfo)
  149.               {
  150.                 STRPTR a;
  151.                 struct xadTextInfo *ti;
  152.  
  153.                 for(ti = xdi->xdi_TextInfo; ti; ti = ti->xti_Next)
  154.                 {
  155.               a = "TextInfo";
  156.               if(ti->xti_Flags & XADTIF_BANNER)
  157.                 a = "Banner";
  158.               else if(ti->xti_Flags & XADTIF_FILEDIZ)
  159.                 a = "DIZ-Text";
  160.  
  161.               if(ti->xti_Size && ti->xti_Text)
  162.                     Printf("There is a %s with size %ld.\n", a, ti->xti_Size);
  163.                   else if(ti->xti_Flags & XADTIF_CRYPTED)
  164.                     Printf("There is a crypted %s.\n", a);
  165.                   else
  166.                     Printf("There is an empty %s.\n", a);
  167.                 }
  168.                 if(args.showtexts)
  169.                   ShowTexts(xdi->xdi_TextInfo);
  170.                 if(args.savetexts)
  171.                   SaveTexts(xdi->xdi_TextInfo, args.savetexts);
  172.               }
  173.               xdi = xdi->xdi_Next;
  174.             }
  175.             ret = 0;
  176.           }
  177.           else
  178.           {
  179.         struct xadDeviceInfo *dvi = 0;
  180.  
  181.             if(args.to[strlen(args.to)-1] == ':' && stricmp(args.to, "NIL:"))
  182.             {
  183.               if((dvi = (struct xadDeviceInfo *)
  184.               xadAllocObjectA(XADOBJ_DEVICEINFO, 0)))
  185.               {
  186.                 args.to[strlen(args.to)-1] = 0; /* strip ':' */
  187.                 dvi->xdi_DOSName = args.to;
  188.               }
  189.               else
  190.                 err = XADERR_NOMEMORY;
  191.             }
  192.             if(args.showtexts || args.savetexts)
  193.             {
  194.               struct xadDiskInfo *xdi = ai->xai_DiskInfo;
  195.  
  196.               while(xdi && xdi->xdi_EntryNumber < *args.entry)
  197.                 xdi = xdi->xdi_Next;
  198.               if(xdi && xdi->xdi_TextInfo)
  199.               {
  200.                 if(args.showtexts)
  201.                   ShowTexts(xdi->xdi_TextInfo);
  202.                 if(args.savetexts)
  203.                   SaveTexts(xdi->xdi_TextInfo, args.savetexts);
  204.               }
  205.             }
  206.             if(!err && !(err = xadDiskUnArc(ai, dvi ? XAD_OUTDEVICE :
  207.             XAD_OUTFILENAME, dvi ? (ULONG) dvi : (ULONG) args.to,
  208.             XAD_ENTRYNUMBER, *args.entry, args.lowcyl ?
  209.             XAD_LOWCYLINDER : TAG_IGNORE, args.lowcyl ? *args.lowcyl :
  210.             0, args.highcyl ? XAD_HIGHCYLINDER : TAG_IGNORE,
  211.             args.highcyl ? *args.highcyl : 0, XAD_OVERWRITE,
  212.             args.overwrite, XAD_IGNOREGEOMETRY, args.ignoregeometry,
  213.             XAD_VERIFY, TRUE, XAD_PROGRESSHOOK, &prhook, TAG_DONE)))
  214.               ret = 0;
  215.             if(dvi)
  216.               xadFreeObjectA(dvi, 0);
  217.           }
  218.           xadFreeInfo(ai);
  219.         } /* xadGetInfo */
  220.  
  221.         xadFreeObjectA(ai, 0);
  222.           } /* xadAllocObject */
  223.         }
  224.         else
  225.           SetIoErr(ERROR_REQUIRED_ARG_MISSING);
  226.  
  227.         FreeArgs(rda);
  228.       } /* ReadArgs */
  229.  
  230.       if(CTRL_C)
  231.         SetIoErr(ERROR_BREAK);
  232.  
  233.       if(err)
  234.     Printf("An error occured: %s\n", xadGetErrorText(err));
  235.       else if(ret)
  236.         PrintFault(IoErr(), 0);
  237.  
  238.       CloseLibrary((struct Library *) xadmasterbase);
  239.     } /* OpenLibrary xadmaster */
  240.     else
  241.       Printf("Could not open xadmaster.library\n");
  242.     CloseLibrary((struct Library *) dosbase);
  243.   } /* OpenLibrary dos */
  244.   return ret;
  245. }
  246.  
  247. ASM(ULONG) SAVEDS progrhook(REG(a0, struct Hook *hook),
  248. REG(a1, struct xadProgressInfo *pi))
  249. {
  250.   ULONG ret = 0;
  251.  
  252.   switch(pi->xpi_Mode)
  253.   {
  254.   case XADPMODE_ASK:
  255.     {
  256.       UBYTE r;
  257.       if(pi->xpi_Status & XADPIF_OVERWRITE)
  258.       {
  259.         Printf("File already exists, overwrite? (Y|S|\033[1mN\033[0m): ");
  260.         Flush(Output());
  261.         SetMode(Input(), TRUE);
  262.         r = FGetC(Input());
  263.         if(r == 'Y' || r == 'y')
  264.           ret |= XADPIF_OVERWRITE;
  265.         else if(r == 'S' || r == 's')
  266.           ret |= XADPIF_SKIP;
  267.         SetMode(Input(), FALSE);
  268.       }
  269.       if(pi->xpi_Status & XADPIF_IGNOREGEOMETRY)
  270.       {
  271.         Printf("Drive geometry not correct, ignore? (Y|S|\033[1mN\033[0m): ");
  272.         Flush(Output());
  273.         SetMode(Input(), TRUE);
  274.         r = FGetC(Input());
  275.         if(r == 'Y' || r == 'y')
  276.           ret |= XADPIF_IGNOREGEOMETRY;
  277.         else if(r == 'S' || r == 's')
  278.           ret |= XADPIF_SKIP;
  279.         SetMode(Input(), FALSE);
  280.       }
  281.     }
  282.     break;
  283.   case XADPMODE_PROGRESS:
  284.     {
  285.       if(pi->xpi_DiskInfo->xdi_Flags & (XADDIF_NOCYLINDERS|XADDIF_NOCYLSECTORS))
  286.       {
  287.         Printf("\r\033[KWrote %ld of %ld bytes (%ld/%ld sectors)",
  288.         pi->xpi_CurrentSize, pi->xpi_DiskInfo->xdi_TotalSectors*
  289.         pi->xpi_DiskInfo->xdi_SectorSize, pi->xpi_CurrentSize/
  290.         pi->xpi_DiskInfo->xdi_SectorSize, pi->xpi_DiskInfo->xdi_TotalSectors);
  291.       }
  292.       else
  293.       {
  294.         ULONG numcyl, fullsize, curcyl, i;
  295.  
  296.         i = pi->xpi_DiskInfo->xdi_CylSectors *
  297.             pi->xpi_DiskInfo->xdi_SectorSize;
  298.         numcyl = pi->xpi_HighCyl+1-pi->xpi_LowCyl;
  299.         fullsize = numcyl * i;
  300.         curcyl = pi->xpi_CurrentSize/i;
  301.  
  302.         Printf("\r\033[KWrote %ld of %ld bytes (%ld/%ld cylinders)",
  303.         pi->xpi_CurrentSize, fullsize, curcyl, numcyl);
  304.       }
  305.       Flush(Output());
  306.     }
  307.     break;
  308.   case XADPMODE_END: 
  309.     if(pi->xpi_DiskInfo->xdi_Flags & (XADDIF_NOCYLINDERS|XADDIF_NOCYLSECTORS))
  310.       Printf("\r\033[KWrote %ld bytes (%ld sectors)\n",
  311.       pi->xpi_CurrentSize, pi->xpi_DiskInfo->xdi_TotalSectors);
  312.     else
  313.       Printf("\r\033[KWrote %ld bytes (%ld cylinders)\n",
  314.       pi->xpi_CurrentSize, pi->xpi_HighCyl+1-pi->xpi_LowCyl);
  315.     break;
  316.   case XADPMODE_ERROR: Printf("\r\033[K");
  317.     break;
  318.   }
  319.  
  320.   if(!CTRL_C) /* clear ok flag */
  321.     ret |= XADPIF_OK;
  322.  
  323.   return ret;
  324. }
  325.  
  326. void ShowTexts(struct xadTextInfo *ti)
  327. {
  328.   ULONG i = 1, j;
  329.   BPTR fh;
  330.   STRPTR a;
  331.  
  332.   fh = Output();
  333.  
  334.   while(!CTRL_C && ti)
  335.   {
  336.     if(ti->xti_Size && ti->xti_Text)
  337.     {
  338.       Printf("»»»» TEXTINFO %ld ««««\n", i);
  339.       a = ti->xti_Text;
  340.       for(j = 0; !CTRL_C && j < ti->xti_Size; ++j)
  341.       {
  342.         if(isprint(*a) || *a == '\n' || *a == '\t' || *a == '\033')
  343.           FPutC(fh, *a);
  344.         else
  345.           FPutC(fh, '.');
  346.         ++a;
  347.       }
  348.       if(*(--a) != '\n')
  349.         FPutC(fh, '\n');
  350.     }
  351.     ti = ti->xti_Next;
  352.     ++i;
  353.   }
  354. }
  355.  
  356. void SaveTexts(struct xadTextInfo *ti, STRPTR name)
  357. {
  358.   UBYTE namebuf[256];
  359.   ULONG i = 1;
  360.   BPTR fh;
  361.   LONG err = 0;
  362.  
  363.   while(!CTRL_C && ti && !err)
  364.   {
  365.     if(ti->xti_Size && ti->xti_Text)
  366.     {
  367.       sprintf(namebuf, "%s.%ld", name, i);
  368.       if((fh = Open(namebuf, MODE_NEWFILE)))
  369.       {
  370.         if(Write(fh, ti->xti_Text, ti->xti_Size) != ti->xti_Size)
  371.           ++err;
  372.         Close(fh);
  373.       }
  374.       else
  375.         ++err;
  376.     }
  377.     ti = ti->xti_Next;
  378.     ++i;
  379.   }
  380.   if(err)
  381.     Printf("Failed to save information texts.\n");
  382. }
  383.